package com.gnerv.sylvanas.framework.plugin.os.tools;

import com.gnerv.sylvanas.framework.plugin.os.model.OSDisk;
import org.springframework.beans.BeanUtils;
import oshi.SystemInfo;
import oshi.hardware.CentralProcessor;
import oshi.hardware.GlobalMemory;
import oshi.software.os.FileSystem;
import oshi.software.os.OSFileStore;
import oshi.software.os.OperatingSystem;

import java.util.ArrayList;
import java.util.List;

public class OsTools {

    public static SystemInfo SYSTEM_INFO = new SystemInfo();
    public static CentralProcessor SYSTEM_PROCESSOR = SYSTEM_INFO.getHardware().getProcessor();
    public static GlobalMemory SYSTEM_MEMORY = SYSTEM_INFO.getHardware().getMemory();

    public static OperatingSystem OPERATING_SYSTEM = SYSTEM_INFO.getOperatingSystem();
    public static FileSystem FILE_SYSTEM = OPERATING_SYSTEM.getFileSystem();
    public static List<OSFileStore> FILE_STORE_LIST = FILE_SYSTEM.getFileStores();


    public static String osName() {
        return OPERATING_SYSTEM.getFamily();
    }

    public static String osVersion() {
        return OPERATING_SYSTEM.getVersionInfo().getVersion();
    }

    public static String osFramework() {
        return OPERATING_SYSTEM.getBitness() + "";
    }

    public static long totalMemory() {
        return SYSTEM_MEMORY.getTotal();
    }

    public static long freeMemory() {
        return SYSTEM_MEMORY.getAvailable();
    }

    public static long usedMemory() {
        return SYSTEM_MEMORY.getTotal() - SYSTEM_MEMORY.getAvailable();
    }

    public static List<OSDisk> diskList() {
        List<OSDisk> osDiskList = new ArrayList<>();
        for (OSFileStore osFileStore : FILE_STORE_LIST) {
            OSDisk osDisk = new OSDisk();
            BeanUtils.copyProperties(osFileStore, osDisk);
            osDiskList.add(osDisk);
        }
        return osDiskList;
    }

}
